package org.jeecg.modules.demo.ch.service.impl;

import org.jeecg.modules.demo.ch.entity.ChDesignBomMaster;
import org.jeecg.modules.demo.ch.entity.ChDesignBomDetail;
import org.jeecg.modules.demo.ch.mapper.ChDesignBomDetailMapper;
import org.jeecg.modules.demo.ch.mapper.ChDesignBomMasterMapper;
import org.jeecg.modules.demo.ch.service.IChDesignBomMasterService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 模具设计BOM主表
 * @Author: jeecg-boot
 * @Date:   2022-09-02
 * @Version: V1.0
 */
@Service
public class ChDesignBomMasterServiceImpl extends ServiceImpl<ChDesignBomMasterMapper, ChDesignBomMaster> implements IChDesignBomMasterService {

	@Autowired
	private ChDesignBomMasterMapper chDesignBomMasterMapper;
	@Autowired
	private ChDesignBomDetailMapper chDesignBomDetailMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(ChDesignBomMaster chDesignBomMaster, List<ChDesignBomDetail> chDesignBomDetailList) {
		chDesignBomMasterMapper.insert(chDesignBomMaster);
		if(chDesignBomDetailList!=null && chDesignBomDetailList.size()>0) {
			for(ChDesignBomDetail entity:chDesignBomDetailList) {
				//外键设置
				entity.setChDesignBomMasterId(chDesignBomMaster.getId());
				chDesignBomDetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(ChDesignBomMaster chDesignBomMaster,List<ChDesignBomDetail> chDesignBomDetailList) {
		chDesignBomMasterMapper.updateById(chDesignBomMaster);
		
		//1.先删除子表数据
		chDesignBomDetailMapper.deleteByMainId(chDesignBomMaster.getId());
		
		//2.子表数据重新插入
		if(chDesignBomDetailList!=null && chDesignBomDetailList.size()>0) {
			for(ChDesignBomDetail entity:chDesignBomDetailList) {
				//外键设置
				entity.setChDesignBomMasterId(chDesignBomMaster.getId());
				chDesignBomDetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		chDesignBomDetailMapper.deleteByMainId(id);
		chDesignBomMasterMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			chDesignBomDetailMapper.deleteByMainId(id.toString());
			chDesignBomMasterMapper.deleteById(id);
		}
	}
	
}
